package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.ListUtils;
import com.ab.util.ToastUtil;
import com.ab.view.scrollview.KJScrollView;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.WaiMaiGoodsListAdapter;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.MarketData;
import com.baidumap.administrator.happystudy.model.WaiMaiGoodsList;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.socks.library.KLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/13 13:41
 * 邮箱：2235445233@qq.com
 */
public class WaiMaiGoodsListActivity extends AppBaseActivity implements SwipyRefreshLayout.OnRefreshListener, OnMessageResponse {

    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);


    public ImageLoader loader;
    @Bind(R.id.list)
    ListView mListView;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private WaiMaiGoodsListAdapter mAdapter;
    private static int currentPage = 1;
    private ArrayList<Map<String, Object>> arrayList;
    @Bind(R.id.main_SwipyRefreshLayout)
    SwipyRefreshLayout mSwipyRefreshLayout;


    private String mCatrgoryId;
    ArrayList<HashMap<String, Object>> result = new ArrayList<>();
    private String mTitle;//标题

    HashMap<String, MarketData> mHashMap = new HashMap<>();

    @Bind(R.id.kj_scrool)
    KJScrollView kj_scrool;

    @Override
    public int bindLayout() {
        return R.layout.activity_wai_mai_goods_list;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();

        iniView();
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(mTitle);
        kj_scrool.scrollTo(0, 0);
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.shopTakeawayCate(this, mCatrgoryId);
    }


    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            mCatrgoryId = bundle.getString("category_id");
            mTitle = bundle.getString("title");
        }
    }

    private void iniView() {
        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(currentPage);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(this);
        // LoadImage初始化
        loader = ImageLoader.getInstance();
        currentPage = 1;
        mAdapter = new WaiMaiGoodsListAdapter(this, loader);

    }


    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {


        if (url.equals(Constant.SHOP_TAKEAWAY_CATE) && jo != null) {
            WaiMaiGoodsList waiMaiGoodsList = new WaiMaiGoodsList(jo.toString());

            if (waiMaiGoodsList != null) {


                if (waiMaiGoodsList.getCode().equals("200")) {

                    List<WaiMaiGoodsList.DataBean> dataBeanList = waiMaiGoodsList.getData();


                    HashMap<String, Object> map = null;

                    for (int i = 0; i < dataBeanList.size(); i++) {


                        map = new HashMap<>();
                        map.put("shop_id", dataBeanList.get(i).getProduct_id());
                        map.put("shop_name", dataBeanList.get(i).getProduct_name());
                        map.put("shop_img", dataBeanList.get(i).getProduct_pic1());
//                        map.put("shop_rating", dataBeanList.get(i).getPrice());
                        map.put("shop_distance", dataBeanList.get(i).getPrice());
                        map.put("shop_walk_time", dataBeanList.get(i).getBuy_numbers());
                        result.add(map);

                    }

                    list.addAll(result);//

                    ListUtils.removeDuplicate(list);

                    currentPageAdd(list);
                    mAdapter.bindData(list);


                    if (currentPage == 1) {

                        mListView.setAdapter(mAdapter);

                    }
                    mAdapter.notifyDataSetChanged();

                    ListUtils.setListViewHeightBasedOnChildren(mListView);
                } else {
                    ToastUtil.showMessage(this, waiMaiGoodsList.getMsg());
                }
            }
            mSwipyRefreshLayout.setRefreshing(false);
        }
    }

    protected void currentPageAdd(ArrayList mListView) {
        KLog.e("========" + currentPage);
        if (mListView.size() % 20 == 0 && mListView.size() > 0) {
            currentPage++;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }

    /**
     * 下拉刷新
     *
     * @param index 当前分页索引
     */
    @Override
    public void onRefresh(int index) {
        currentPage = 1;

        result.clear();
        list.clear();
        mAdapter.notifyDataSetChanged();

        Api.shopTakeawayCate(this, mCatrgoryId);
    }

    /**
     * 加载更多
     *
     * @param index 当前分页索引
     */
    @Override
    public void onLoad(int index) {
        //mSwipyRefreshLayout.setRefreshing(false);


    }
}
